package com.xkdx.guangguang.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

public class SinaWeiboUtils {
	/**
	 * 将text中@某人、#某主题、http://网址的字体加亮，匹配的表情文字以表情显示
	 * 
	 * @param text
	 * @param context
	 * @return
	 * @author lvqiyong
	 */

	public static SpannableString formatContentNoClick(CharSequence text) {
		SpannableString spannableString = new SpannableString(text);

		Pattern pattern = Pattern
				.compile("@[^ \\.^\\,^:^;^!^(^)^\\?^\\s^#^@^。^，^：^；^！^？^（^）]+|#(.+?)#|http://t\\.cn/\\w+|\\[(.*?)\\]");
		Matcher matcher = pattern.matcher(spannableString);
		while (matcher.find()) {
			final String match = matcher.group();
			if (match.startsWith("@")) { // @某人，加亮字体
				spannableString.setSpan(new ForegroundColorSpan(0xff8b6c4d), matcher.start(), matcher.end(),
						Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			} else if (match.startsWith("#")) { // #某主题
				spannableString.setSpan(new ForegroundColorSpan(0xff8b6c4d), matcher.start(), matcher.end(),
						Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			}

		}
		return spannableString;
	}

	public static long calculateWeiboLength(CharSequence c) {

		double len = 0;
		for (int i = 0; i < c.length(); i++) {
			int temp = (int) c.charAt(i);
			if (temp > 0 && temp < 127) {
				len += 0.5;
			} else {
				len++;
			}
		}
		return Math.round(len);
	}
}
